<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .red {
      color: red;
    }

    table {
      width: 100%;
      border-collapse: collapse;
    }

    th,
    td {
      padding: 10px;
      text-align: left;
      border-bottom: 1px solid #ccc;
    }

    input[type="text"] {
      padding: 5px;
      border: 1px solid #ccc;
      border-radius: 4px;
    }

    button {
      padding: 10px;
      background-color: #007bff;
      color: #fff;
      border: none;
      border-radius: 4px;
      cursor: pointer;
    }

    button:hover {
      background-color: #0056b3;
    }
  </style>

</head>

<body>
  <div id="app">
    <table>
      <thead></thead>
      <tbody v-if="list.length > 0">
        <tr v-for="(item, index) in list " :key="item.id">
          <td>{{index + 1}}: </td>
          <td>{{item.subject}}</td>
          <!-- 小于60的为不及格 设置为红色的 -->
          <td :class="{red: item.score < 60}">{{item.score}}</td>
          <td><a @click.prevent="del(item.id)" href="">删除</a></td>
        </tr>
      </tbody>
      <tbody v-if="list.length == 0">暂无数据</tbody>
      <tfoot>
        总分：{{totalScore}}
        平均分：{{averageScore}}
      </tfoot>
    </table>

    科目: <input type=" text" placeholder="请输入科目" v-model.trim="subject"><br><br>
    分数：<input type="text" placeholder="请输入分数" v-model.number="score"><br><br>
    <button @click="submit">添加</button>
  </div>
  <script src="../vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        list: [
          { id: 1, subject: '语文', score: 20 },
          { id: 7, subject: '数学', score: 99 },
          { id: 12, subject: '英语', score: 70 },
        ],
        subject: '',
        score: ''
      },
      methods: {
        del(id) {
          this.list = this.list.filter(item => item.id != id)
        },
        submit() {
          if (this.submit == '') {
            alert('请输入科目')
            return
          }
          if (typeof this.score != 'number') {
            // alert(typeof this.score)
            alert('请输入正确的成绩')
            return
          }
          this.list.unshift({
            id: +new Date(),
            subject: this.subject,
            score: this.score
          })
          this.subject = ' '
          this.score = ''
        }
      },
      computed: {
        totalScore() {
          return this.list.reduce((sum, item) => sum + item.score, 0)
        },
        averageScore() {
          if (this.totalScore == 0) return 0
          return (this.totalScore * 1.0 / this.list.length).toFixed(2)
        }
      }
    })
  </script>
</body>

</html>